package transportManagement.servlet;

import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import accountManagement.Client;
import accountManagement.Employee;
import transportManagement.TransportBoard;
import transportManagement.Transport;

/**
 * Servlet implementation class RicercaCorsa
 */
@WebServlet("/ServletSearchTransport")
public class ServletSearchTransport extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletSearchTransport() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

    private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	try {
    		Object user = request.getSession().getAttribute("user");
        	if(user == null || (!(user instanceof Client) && !(user instanceof Employee))) {
        		throw new InvalidParameterException("Non disponi dei privilegi necessari per accedere alla pagina richiesta");
        	}
    		// Parametri della ricerca
    		String transportId = request.getParameter("transportId");
    		String origin = request.getParameter("origin");
    		String destination = request.getParameter("destination");
    		
    		// Creazione del contenuto della risposta
    		List<Transport> transportList;
    		if(transportId.isEmpty() && origin.isEmpty() && destination.isEmpty()) {
    			transportList = TransportBoard.getInstance().getTransportList();
    		} else {
    			Transport transport = new Transport();
    			if(!transportId.isEmpty())
    				transport.setTransportId(transportId);
    			if(!origin.isEmpty())
    				transport.setOrigin(origin);
    			if(!destination.isEmpty())
    				transport.setDestination(destination);
    			
    			transportList = TransportBoard.getInstance().search(transport);
    		}
    		
    		request.setAttribute("transportList", transportList);
    		request.getRequestDispatcher("/transport/search.jsp").forward(request, response);
    	} catch(InvalidParameterException | NullPointerException e) {
			request.setAttribute("error", e.getMessage());
    		request.getRequestDispatcher("/errorPage.jsp").forward(request, response);
		}
    }
}
